package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import br.com.edumobi.P;
import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;

public class LoginFragment extends GenericoFragment {

    private TextView textViewNomeTelaLoginFragment;
    private TextView textViewEsqueciSenhaLoginFragment;
    private EditText editTextEmailLoginFragment;
    private EditText editTextSenhaLoginFragment;
    private Button buttonEntrarLoginFragment;
    private Button buttonCadastreseLoginFragment;
    private ProgressBar progressBarLoginFragment;

    private AcoesLoginFragment activity;
    private LoginTask task;

    View.OnClickListener clickButtonEntrarLoginFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = editTextEmailLoginFragment.getText().toString();
            String senha = editTextSenhaLoginFragment.getText().toString();

            if (email.isEmpty()) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_email);
            } else if (senha.isEmpty()) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_senha);
            } else {
                if (email.equals(activity.obterPreferencesEmail()) && senha.equals(activity.obterPreferencesSenha())) {
                    if (activity.conectado()) {
                        task = new LoginTask();
                        task.execute();
                    } else {
                        activity.irDashboard(activity.obterPreferencesAluno());
                    }
                } else {
                    if (activity.conectado()) {
                        task = new LoginTask();
                        task.execute();
                    } else {
                        activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
                    }
                }
            }
        }
    };

    View.OnClickListener clickButtonCadastreseLoginFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.irCadastrese();
        }
    };

    View.OnClickListener clickTextViewEsqueciSenhaLoginFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.irEsqueciASenha();
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = (AcoesLoginFragment) getActivity();

        String email = activity.obterPreferencesEmail();
        String senha = activity.obterPreferencesSenha();

        editTextEmailLoginFragment.setText(email);
        editTextSenhaLoginFragment.setText(senha);

        if (email != null && !email.trim().equals(P.STRING_EMPITY) && senha != null && !senha.trim().equals(P.STRING_EMPITY)) {
            task = new LoginTask();
            task.execute();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);

        textViewNomeTelaLoginFragment = (TextView) view.findViewById(R.id.textViewNomeTelaLoginFragment);
        textViewEsqueciSenhaLoginFragment = (TextView) view.findViewById(R.id.textViewEsqueciSenhaLoginFragment);
        editTextEmailLoginFragment = (EditText) view.findViewById(R.id.editTextEmailLoginFragment);
        editTextSenhaLoginFragment = (EditText) view.findViewById(R.id.editTextSenhaLoginFragment);
        buttonEntrarLoginFragment = (Button) view.findViewById(R.id.buttonEntrarLoginFragment);
        buttonCadastreseLoginFragment = (Button) view.findViewById(R.id.buttonCadastreseLoginFragment);
        progressBarLoginFragment = (ProgressBar) view.findViewById(R.id.progressBarLoginFragment);

        buttonEntrarLoginFragment.setOnClickListener(clickButtonEntrarLoginFragment);
        buttonCadastreseLoginFragment.setOnClickListener(clickButtonCadastreseLoginFragment);
        textViewEsqueciSenhaLoginFragment.setOnClickListener(clickTextViewEsqueciSenhaLoginFragment);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            esconderComponentes();
            progressBarLoginFragment.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void exibirComponentes() {
        textViewNomeTelaLoginFragment.setVisibility(View.VISIBLE);
        editTextEmailLoginFragment.setVisibility(View.VISIBLE);
        editTextSenhaLoginFragment.setVisibility(View.VISIBLE);
        buttonEntrarLoginFragment.setVisibility(View.VISIBLE);
        buttonCadastreseLoginFragment.setVisibility(View.VISIBLE);
        textViewEsqueciSenhaLoginFragment.setVisibility(View.VISIBLE);
        progressBarLoginFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewNomeTelaLoginFragment.setVisibility(View.GONE);
        editTextEmailLoginFragment.setVisibility(View.GONE);
        editTextSenhaLoginFragment.setVisibility(View.GONE);
        buttonEntrarLoginFragment.setVisibility(View.GONE);
        buttonCadastreseLoginFragment.setVisibility(View.GONE);
        textViewEsqueciSenhaLoginFragment.setVisibility(View.GONE);
        progressBarLoginFragment.setVisibility(View.GONE);
    }

    private class LoginTask extends AsyncTask<Void, Void, Aluno> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarLoginFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Aluno doInBackground(Void... params) {
            return activity.entrar(editTextEmailLoginFragment.getText().toString(), editTextSenhaLoginFragment.getText().toString());
        }

        @Override
        protected void onPostExecute(Aluno aAluno) {
            exibirComponentes();
            progressBarLoginFragment.setVisibility(View.GONE);

            if (aAluno != null) {
                activity.irDashboard(aAluno);
                activity.salvarPreferencesAluno(aAluno);
                activity.finalizar();
            } else {
                activity.falarEExibirTexto(R.string.excecao_autenticacao_mausucedida);
            }
        }
    }

    public interface AcoesLoginFragment extends AcoesGenericoFragment {
        Aluno entrar(String email, String senha);
        void irCadastrese();
        void irEsqueciASenha();
        void irDashboard(Aluno aluno);
        String obterPreferencesEmail();
        String obterPreferencesSenha();
    }
}
